package lc.oa.ui.fragment;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lc.oa.R;
import lc.oa.model.Schedule;
import lc.oa.ui.adapter.ScheduleAdapter;
import lc.oa.ui.base.BaseFragment;

/**
  *
  * @Description:     工作日历
  * @Author:         lucifer
  * @CreateDate:     2020/8/7 16:14
  * @UpdateUser:
  * @UpdateDate:
  * @UpdateRemark:
  * @Version:        1.0
 */
public class CalendarFragment extends BaseFragment implements CalendarView.OnCalendarSelectListener {
    private TextView myMonthTxt;
    private  CalendarView mCalendarView;
    private ListView lv;
    private FloatingActionButton addSchedlue;
    private String selectDate;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);
        myMonthTxt=view.findViewById(R.id.month_year_txt);
        mCalendarView=view.findViewById(R.id.calendarView);
        lv=view.findViewById(R.id.list_view);
        addSchedlue=view.findViewById(R.id.addSchedule);
        List<lc.oa.model.Calendar> calendarDateList=new ArrayList<>();
        calendarDateList.add(new lc.oa.model.Calendar(2,"2020-08-01"));
        calendarDateList.add(new lc.oa.model.Calendar(0,"2020-08-05"));
        calendarDateList.add(new lc.oa.model.Calendar(1,"2020-08-12"));
        calendarDateList.add(new lc.oa.model.Calendar(1,"2020-08-20"));
        calendarDateList.add(new lc.oa.model.Calendar(1,"2020-08-31"));
        initData(calendarDateList);
        creatMethod();
        steAdapter();

        return view;
    }
    //日程添加适配器
    private void steAdapter() {
        List<Schedule>  scheduleList= initLoad();
        ScheduleAdapter sa=new ScheduleAdapter(getContext(),scheduleList);
        lv.setAdapter(sa);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Schedule map= (Schedule) scheduleList.get(position);
//
                Toast.makeText(getActivity(), map.getId()+"", Toast.LENGTH_SHORT).show();
            }
        });
    }
    private List<Schedule> initLoad(){
        List<Schedule>  slist=new ArrayList<>();
        slist.add(new Schedule(11111,"上午8:00","下午3:00","hahahahahafdfdhsfdskfdskjfhshfdsf鬼地方个梵蒂冈减肥抵抗力"));
        slist.add(new Schedule(22222,"上午8:00","下午3:00","小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会"));
        slist.add(new Schedule(33333,"上午8:00","下午3:00","新需求阅读研究，发现问题，并找组 员讨论可能会遇到的问题并给出解决 思路"));
        slist.add(new Schedule(44444,"上午8:00","下午3:00","程序极速开发大赛颁奖大会"));
        slist.add(new Schedule(55555,"上午8:00","下午3:00","品新需求讲解会力"));
        slist.add(new Schedule(66666,"上午8:00","下午3:00","序极速开发大赛颁奖大会"));
        slist.add(new Schedule(11111,"上午8:00","下午3:00","hahahahahafdfdhsfdskfdskjfhshfdsf鬼地方个梵蒂冈减肥抵抗力"));
        slist.add(new Schedule(22222,"上午8:00","下午3:00","小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会小程序极速开发大赛颁奖大会"));
        slist.add(new Schedule(33333,"上午8:00","下午3:00","新需求阅读研究，发现问题，并找组 员讨论可能会遇到的问题并给出解决 思路"));
        slist.add(new Schedule(44444,"上午8:00","下午3:00","程序极速开发大赛颁奖大会"));
        slist.add(new Schedule(55555,"上午8:00","下午3:00","品新需求讲解会力"));
        slist.add(new Schedule(66666,"上午8:00","下午3:00","序极速开发大赛颁奖大会"));

        return slist;
    }
    //初始化方法
    private void creatMethod() {
        mCalendarView.setOnCalendarSelectListener(this);
        addSchedlue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(selectDate==null){
                    selectDate=  mCalendarView.getCurYear()+"-"+ mCalendarView.getCurMonth()+"-"+mCalendarView.getCurDay();
                }
                Toast.makeText(getContext(), "点击新增"+selectDate, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData(List<lc.oa.model.Calendar> calendarDateList)  {

        Map<String, Calendar> map = new HashMap<>();

        for (int i=0;i<calendarDateList.size();i++){
            Date date = null;
            try {
                date = new SimpleDateFormat("yyyy-MM-dd").parse(calendarDateList.get(i).getDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(date);
            int selYear = c.get(java.util.Calendar.YEAR);
            int selMOnth = c.get(java.util.Calendar.MONTH) + 1; // 0-based!
            int day = c.get(java.util.Calendar.DAY_OF_MONTH);
            Log.e("时间",selYear+"-"+selMOnth+"-"+day);
            map.put(getSchemeCalendar(selYear, selMOnth, day, 0xFFFFB44B, "休",calendarDateList.get(i).getType()).toString(),
                    getSchemeCalendar(selYear, selMOnth, day, 0xFFFFB44B, "休",calendarDateList.get(i).getType()));
        }

        //此方法在巨大的数据量上不影响遍历性能，推荐使用
        mCalendarView.setSchemeDate(map);
    }
    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text,int type) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.addScheme(type,color, text);

        return calendar;
    }
    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        Log.e("选中",calendar.getYear()+"---"+calendar.getMonth()+"----"+calendar.getDay());
        myMonthTxt.setText(calendar.getYear()+"."+(calendar.getMonth()<10?"0"+calendar.getMonth():calendar.getMonth()));
        selectDate=calendar.getYear()+"-"+calendar.getMonth()+"-"+calendar.getDay();
        if(isClick){
            Toast.makeText(getContext(), selectDate, Toast.LENGTH_SHORT).show();
        }else{
            if(calendar.getMonth()==9){
//                String dataArr[]={"2020-09-02","2020-09-12","2020-09-18","2020-09-22","2020-09-28"};
//                initData(dataArr);
            }

        }
    }
    @Override
    protected void handlerMessage1(Message msg) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initControl() {

    }

    @Override
    public void onModeChanged(int action, Object... values) {

    }

}
